﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Albon.Model;
namespace AlbonWeb.Order
{
    public partial class Default : System.Web.UI.Page
    {
        private readonly string username = HttpContext.Current.User.Identity.Name;
        private readonly BLL.OrderBLL orderbll = new BLL.OrderBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request["check"] == null)
                {
                    checkOrder();
                }
                bindDDL(ddlClient);  
                
            }
        }
        //判断是否有未完成的订单
        protected void checkOrder()
        {
            BLL.OrderBLL orderbll = new BLL.OrderBLL();
            DataTable dtnew=orderbll.getNewOrder(username);
            if (dtnew.Rows.Count != 0)
            {
                string refNo = dtnew.Rows[0]["refNo"].ToString();
                string clientName = dtnew.Rows[0]["clientName"].ToString();
                Session["orderid"] = dtnew.Rows[0]["ID"].ToString();
                Commonlib.JavaScriptHelper.ConfirmGoTo("The order '"+refNo+"' for "+clientName+" is unfinished ,Continue?",
                    "/Order/OrderItemList.aspx", "/Order/Default.aspx?check=0");
            }
            else
            { 
                
            }
        }
        protected void btnNextA_Click(object sender, EventArgs e)
        {
            //将订单号，用户名及客户名称存入订单表finished=0
            OrderInfo order = new OrderInfo();
            order.userName = username;
            order.refNo = refNo.Text.Trim();
            order.clientName = ddlClient.SelectedItem.Text;
            //添加新订单，删除原临时订单项中的数据
            string orderid=orderbll.addNewOrder(order);
            if (orderid!="0")
            {
                Response.Redirect("/Manager/ClientManager/ClientPosture.aspx");
            }
        }

        protected void btnNextB_Click(object sender, EventArgs e)
        {
            BLL.ClientBLL clientbll = new BLL.ClientBLL();
            Client client = new Client();
            client.userName = username;
            client.clientName = clientName.Text.Trim();
            client.sex = ddlGender.SelectedValue=="1";
            client.monoName = monoName.Text.Trim();
            clientbll.addClients(client);
            OrderInfo orderinfo = new OrderInfo();
            orderinfo.userName = username;
            orderinfo.refNo = refNo.Text.Trim();
            orderinfo.clientName = client.clientName;
            orderbll.addNewOrder(orderinfo);
            Response.Redirect("/Manager/ClientManager/ClientPosture.aspx");
        }
        protected void bindDDL(DropDownList ddl)
        {
            BLL.ClientBLL clientbll = new BLL.ClientBLL();
            ddl.DataSource=clientbll.getClientsByUserName(username);
            ddl.DataTextField = "clientName";
            ddl.DataValueField = "ID";
            ddl.DataBind();
            ddlClient.Items.Insert(0, "--Select Client--");
        }
    }
}